<?php
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8">';
/*
* project
*
*/
  class  common{

private $root_path;

function __construct($root_path){
include_once $root_path . 'admin/class/cfg.class.php';
include_once $root_path.'admin/class/company.class.php';
include_once $root_path.'admin/class/temp.class.php';
include_once $root_path.'includes/conn.php';
$this->set_root_path($root_path);
}
function get_temp_url(){
$root_path=$this->get_root_path();
$cfg=new cfg($root_path);
  $temp=$cfg->xml_read('temp');
 $url=$root_path.'templates/'.$temp.'/';

return $url;
}

/*
* print head() for all pages
*/
function do_head($title){
/*
*  get the data from xml which modified lastest
*/
$root_path=$this->get_root_path();
$cfg=new cfg($root_path);
  $meta_title=$cfg->xml_read('site_name');
 $meta_key=$cfg->xml_read('meta_key');
 $meta_des=$cfg->xml_read('meta_des');
 $url=$this->get_temp_url();
   $close=$cfg->xml_read('close');
     if($close==1){
   $info=$cfg->xml_read('close_info');
   echo $info;
   exit;
   }
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="title" content="<?php echo $meta_title; ?>">
<meta name="description" content="<?php echo $meta_des; ?>">
<meta name="keywords" content="<?php echo $meta_key; ?>">
<link rel="stylesheet" type="text/css" href="<?php echo $url; ?>css/page.css" media="all" />
<title><?php echo $title ?></title>
<link href="<?php echo $url; ?>css/default.css" rel="stylesheet"
	type="text/css">
<script language=javascript src="<?php echo $url; ?>js/scriptfounc.js"></script>
<script language=javascript src="<?php echo $url; ?>js/common.js"></script>
</head>
<?php

}

/*
 * get company  from xml
 */
 function get_company(){
 	$company=array();
$root_path=$this->get_root_path();
$demo=new company($root_path);
$company['name']=$demo->xml_read('name');
$company['notice']=$demo->xml_read('notice');
$company['phone']=$demo->xml_read('phone');
$company['zip_code']=$demo->xml_read('zip_code');
$company['linkman']=$demo->xml_read('linkman');
$company['mail']=$demo->xml_read('mail');
$company['address']=$demo->xml_read('address');
$company['icp']=$demo->xml_read('icp');
 	return $company;
 }
/*
 * 获取产品函数  在后台设定产品数目
 * $fun 1 image  0 data
 */
function get_pros($fun){
$pros='';
$root_path=$this->get_root_path();
$demo=new temp($root_path);
$conn=new conn();
if($fun){
$max=$demo->xml_read('pros_image');
$sql=sprintf("select id,name,image from products limit 0,%d",$max);
}else{
$max=$demo->xml_read('pros_data');
$sql=sprintf("select id,name,date from products  limit 0,%d",$max);
}
$conn->set_sql($sql);
$line=$conn->get_line();
if($line){
$result=$conn->get_result();
for($i=0;$row=$result->fetch_row();$i++){
	$pros[$i]['id']=$row[0];
	$pros[$i]['name']=$row[1];
	$pros[$i]['con']=$row[2];
}
}else{
	$newst='暂无产品';
}

return $pros;
}
/*
*  获取所有商品种类
*/
function get_sorts(){
$sorts=array();
$conn=new conn();
$sql="select id,name from products_sort";
$conn->set_sql($sql);
$line=$conn->get_line();
if($line){
$result=$conn->get_result();
for($i=0;$row=$result->fetch_row();$i++){
$sorts[$i]['id']=$row[0];
$sorts[$i]['name']=$row[1];
}
}else{
$sorts='暂无分类';
}
return $sorts;
}
/*
 * 获取单个（多个）产品详细信息
 */

function get_pro($id){
$pro='';
$id=$id;
$conn=new conn();

$sql=sprintf("select products.id, products_sort.name ,products.name,introduction,date from products join products_sort where products.id='%d' and sort_id=products_sort.id ",$id);
$conn->set_sql($sql);
$line=$conn->get_line();
if($line){
$result=$conn->get_result();
for($i=0;$row=$result->fetch_row();$i++){
	$pro['id']=$row[0];
	$pro['sort']=$row[1];
	$pro['name']=$row[2];
		$pro['intro']=$row[3];
		$pro['date']=$row[4];

}
}else{
	$pro='暂无产品';
}

return $pro;
}


/*
 * 获取新闻函数  在后台设定产品数目
 *
 *  $fun 1 image  0 data
 *
 */
function get_news($fun){
$news='';
$root_path=$this->get_root_path();
$demo=new temp($root_path);
$conn=new conn();
if($fun){
$max=$demo->xml_read('news_image');
$sql=sprintf("select id,title,images from news where verify=1 limit 0,%d",$max);
}else{
$max=$demo->xml_read('news_data');
$sql=sprintf("select id,title,date from news where verify=1 limit 0,%d",$max);
}
$conn->set_sql($sql);
$line=$conn->get_line();
if($line){
$result=$conn->get_result();
for($i=0;$row=$result->fetch_row();$i++){
	$news[$i]['id']=$row[0];
	$news[$i]['title']=$row[1];
	$news[$i]['con']=$row[2];
}
}else{
	$newst='暂无新闻';
}


 	return $news;


}

/*
 * 获取单个（多个）新闻详细信息
 */

function get_new($id){
$new='';

$conn=new conn();
$sql=sprintf("select id,title,comments,author,date from news where verify=1 and id='%d' ",$id);
$conn->set_sql($sql);
$line=$conn->get_line();
if($line){
$result=$conn->get_result();
for($i=0;$row=$result->fetch_row();$i++){
	$new['id']=$row[0];
	$new['title']=$row[1];
	$new['con']=$row[2];
	$new['author']=$row[3];
	$new['date']=$row[4];

}
}else{
	$new='暂无新闻';
}

return $new;
}
/*
 * get guests
 * 1 for commnet 0 for replay
 */

 function get_guests($fun){
$guests='';
$root_path=$this->get_root_path();
$demo=new temp($root_path);
$g_max=$demo->xml_read('guest_num');
$r_max=$demo->xml_read('replay_num');
$conn=new conn();
if($fun){
	$sql=sprintf("SELECT id,comments,date FROM guest_book where verify=1 ORDER BY id DESC LIMIT 0 , %d ",$g_max);
}else{
	$sql=sprintf("SELECT id,replay,date FROM guest_book where verify=1 and replay is not null  ORDER BY re_date DESC LIMIT 0 , %d ",$r_max);
}
$conn->set_sql($sql);
$line=$conn->get_line();
if($line){
$result=$conn->get_result();
for($i=0;$row=$result->fetch_row();$i++){
	$guests[$i]['id']=$row[0];
	$guests[$i]['con']=$row[1];
	$guests[$i]['date']=$row[2];
}
}else{
	$guest='暂无留言';
}
 	return $guests;
 }

 /*
  * get guest single
  */
function get_guest($ids){
$guest='';

	return $guest;
}

/*
 * get votes
 */
function get_votes(){
$votes='';
$root_path=$this->get_root_path();
$demo=new temp($root_path);
$id=$demo->xml_read('vote_num');
$conn=new conn();
$sql=sprintf("SELECT vote_se.id , se_id,vote_id,topic,contant FROM `vote_se` join vote where vote_id=vote.id=%d LIMIT 0 ,7 ",$id);
$conn->set_sql($sql);
$line=$conn->get_line();
	if($line){
			$result=$conn->get_result();
		for($i=0;$row=$result->fetch_row();$i++){
			$votes[$i]['id']=$row[0];
		$votes[$i]['se_id']=$row[1];
		$votes[$i]['vote_id']=$row[2];
		$votes[0]['topic']=$row[3];
		$votes[$i]['contant']=$row[4];
	}
	}else{
	echo	'暂无投票';
	}
	return $votes;
}

/*
 * get flink
 */
 function get_flinks(){
 	$flinks=array();
$root_path=$this->get_root_path();
$demo=new temp($root_path);
$max=$demo->xml_read('flink_num');
$conn=new conn();
$sql=sprintf("select name,images,url,introduction from flinks where verify=1 limit 0,%d",$max);
  $conn->set_sql($sql);
$line=$conn->get_line();
if($line){
$result=$conn->get_result();
for($i=0;$row=$result->fetch_row();$i++){
	$flinks[$i]['name']=$row[0];
	$flinks[$i]['images']=$row[1];
	$flinks[$i]['url']=$row[2];
	$flinks[$i]['introduction'] =$row[3];
}
}else{
	$flinks='暂无友情链接';
}
return $flinks;
 }
/*
 * 搜索功能支持
 */
function search(){


}
/*
*  计数器功能
*/
function get_count(){
$root_path=$this->get_root_path();
include_once $root_path.'admin/class/count.class.php';
include_once $root_path.'admin/class/count_cfg.class.php';
/*
 * init the class about count
 */
$count = new count($root_path);
$count_cfg = new count_cfg($root_path);
$cfg = $count_cfg->count_cfg_get();
$start = $cfg->get_start();
$temp = $cfg->get_temp();
$time = $cfg->get_time();
/*
 * count date get
 */
$a[0] = 0;
$a[1] = 1;
$co = $count->count_read($a);
$count_num = $co[0]->get_id();

$count_num += $start;

$arr = $count->counts_display($count_num);
/*
*  count temp
*/

$img_url = 'images/count/' . $temp . '/';
for ($i = 0; $i < count($arr); $i++) {
	$images.= '<img src="' . $img_url . $arr[$i] . '">';
}

return $images;
}
/*
*  count add
*/
function count_add($page){


$root_path=$this->get_root_path();
include_once $root_path.'admin/class/count.class.php';


$count = new count($root_path);

/* get user's ip*/
$ip = getenv('REMOTE_ADDR');

$bool = $count->count_check($ip, $time);


if ($bool) {
	$line = $count->count_add($ip, $page);
	if ($line) {
		//	echo 'success';
	} else {
		echo 'count add fail';
		exit;
	}
} else {
	echo 'processing was not allowed in ' . $time . ' Seconds';
//	exit;
}



}

/*
*   products 页
*/
function get_products($sort_id=‘0’,$arr){

$products=array();
$conn=new conn;

if($sort_id=='0'){
$sql=sprintf("select products.id, products_sort.name,products.name,image,introduction,date from products join products_sort where products.sort_id=products_sort.id limit %d,%d",$arr[0],$arr[1]);
}else{
$sql=sprintf("select products.id, products_sort.name,products.name,image,introduction,date from products join products_sort where products.sort_id=products_sort.id and sort_id='%d' limit %d,%d",$sort_id,$arr[0],$arr[1]);
}
$conn->set_sql($sql);

$line=$conn->get_line();
if($line){
$result=$conn->get_result();
for($i=0;$row=$result->fetch_row();$i++){
$products[$i]['id']=$row[0];
$products[$i]['sort']=$row[1];
$products[$i]['name']=$row[2];
$products[$i]['image']=$row[3];
$products[$i]['intro']=$row[4];
$products[$i]['date']=$row[5];
} 

}else{
$products='暂无商品';
}
return $products;
}
/*
	 * function for vars which used above
	 */
	function set_root_path($url) {
		$this->root_path = $url;
	}
	function get_root_path() {
		return $this->root_path;
	}


}